package gttp.nspa.client.adm.activity;

import gttp.nspa.client.adm.place.CustomerListPlace;
import gttp.nspa.client.adm.place.CustomerPlace;
import gttp.nspa.client.adm.rpc.ICustomerServiceAsync;
import gttp.nspa.shared.om.Customer;

import java.util.List;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;

public class CustomerListActivity extends AbstractActivity implements ICustomerList.Presenter {

    @Inject
    private ICustomerList         ui;

    @Inject
    private PlaceController       placec;

    @Inject
    private ICustomerServiceAsync service;

    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        ui.setPresenter(this);
        panel.setWidget(ui);
    }

    public void onCreateButtonClicked() {
        placec.goTo(new CustomerPlace());
    }

    public void onEditCustomerRequest(Customer cust) {
        placec.goTo(new CustomerPlace(cust));
    }

    public void onListCustomerRequest(int offset, int limit, String[] orders, final AsyncCallback<List<Customer>> cb) {
        service.listBy(offset, limit, orders, cb);
    }

    public void withPlace(CustomerListPlace place) {
        if (place.reload) {
            ui.reload(false);
        }
    }
}
